//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using OpenLS.Core.Serialization;

namespace OpenLS.Spreadsheet
{
    public class DataConsolidate: IOfficeXmlSerializable
    {
        private readonly List<DataReference> _dataRefs = new List<DataReference>();
        #region IOfficeXmlSerializable Members

        const string xmlName = "dataConsolidate";
        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read(xmlName))
            {
                Function = c.GetOptionalEnum("function", DataConsolidateFunction.Sum);
                LeftLabels = c.GetOptionalBoolean("leftLabels", false);
                TopLabels = c.GetOptionalBoolean("topLabels", false);
                Link = c.GetOptionalBoolean("link", false);
                DataReferences.Clear();
                DataReferences.AddRange(c.ReadOptionalListElement<DataReference>("dataRefs", "dataRef"));
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write(xmlName))
            {
                c.SetOptionalEnum("function", Function, DataConsolidateFunction.Sum);
                c.SetOptionalBoolean("leftLabels", LeftLabels, false);
                c.SetOptionalBoolean("topLabels", TopLabels, false);
                c.SetOptionalBoolean("link", Link, false);
                if (DataReferences.Count > 0)
                {
                    using (var c2 = c.Write("dataRefs"))
                    {
                        c2.WriteElements(DataReferences);
                    }
                }
            }
        }

        #endregion
        private DataConsolidateFunction _function;

        public DataConsolidateFunction Function
        {
            get { return _function; }
            set { _function = value; }
        }
        private bool _leftLabels;

        public bool LeftLabels
        {
            get { return _leftLabels; }
            set { _leftLabels = value; }
        }
        private bool _topLabels;

        public bool TopLabels
        {
            get { return _topLabels; }
            set { _topLabels = value; }
        }
        private bool _link;

        public bool Link
        {
            get { return _link; }
            set { _link = value; }
        }

        public List<DataReference> DataReferences
        {
            get { return _dataRefs; }
        }
    }
}
